package qp.src.app.client;

import qp.src.app.client.widget.PagePopup;
import qp.src.app.shared.DataBeanProxy;
import qp.src.app.shared.DataBeanRequest;
import qp.src.app.shared.DataGridRequestFactory;
import qp.src.app.shared.SubDataBeanProxy;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class AddPopup extends PagePopup {

	private TextBox nameText;
	private TextBox lastNameText;
  private TextBox subNameText;
	private TextBox subLastNameText;
	private TextArea addressText;
	private DateBox dateBox;

	private Button okBtn;
	private Button closeBtn;

	private DataGridRequestFactory factory;

	public AddPopup() {
		super();
		init();
	}

	@Override
	protected void internalInitPage(VerticalPanel layout) {
		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.setWidth("100%");

		nameText = new TextBox();
		nameText.setTitle("Name");
		nameText.ensureDebugId("nameText");

		lastNameText = new TextBox();
		lastNameText.setTitle("Last Name");
		addressText = new TextArea();
		addressText.setTitle("Address");
		dateBox = new DateBox();
		dateBox.setTitle("DateBox");
		DateTimeFormat dateFormat = DateTimeFormat.getShortDateFormat();
		dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));

    subNameText = new TextBox();
    subNameText.setTitle("Sub Name");
    subNameText.ensureDebugId("subNameText");

    subLastNameText = new TextBox();
    subLastNameText.setTitle("Sub Last Name");

		okBtn = new Button("ตกลง");
		closeBtn = new Button("close");

		okBtn.addClickHandler(okHandler);
		closeBtn.addClickHandler(cancelHandler);
		buttonPanel.add(okBtn);
		buttonPanel.add(closeBtn);



		layout.add(nameText);
		layout.add(lastNameText);
		layout.add(addressText);
		layout.add(dateBox);
    layout.add(subNameText);
    layout.add(subLastNameText);
		layout.add(buttonPanel);
	}

	@Override
	protected String getPopupTitle() {
		return "เพิ่ม";
	}

	@Override
	protected void onPageOpen() {
	  Object [] objects = (Object[])getPopupParameter();
	  factory = (DataGridRequestFactory)objects[0];

		nameText.setValue("");
		lastNameText.setValue("");
		addressText.setValue("");
		dateBox.setValue(null);
    subNameText.setValue("");
    subLastNameText.setValue("");
	}

	@Override
	protected void onPageClose() {
		// TODO Auto-generated method stub

	}

//******************************************************************************
	private ClickHandler okHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
		  DataBeanRequest request = factory.dataBeanRequest();
		  DataBeanProxy proxy = request.create(DataBeanProxy.class);
		  proxy.setName( nameText.getValue() );
		  proxy.setLastName( lastNameText.getValue() );
		  proxy.setAddress( addressText.getValue() );
		  proxy.setDateTime( dateBox.getValue() );

      SubDataBeanProxy subDataProxy = request.create(SubDataBeanProxy.class);
      subDataProxy.setName(subNameText.getValue());
      subDataProxy.setLastName(subLastNameText.getValue());
      proxy.setData( subDataProxy );

		  request.persist(proxy).fire(new Receiver<Void>() {

        @Override
        public void onSuccess(Void response) {
          closePopup();
        }
      });
		}
	};

	private ClickHandler cancelHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
			closePopup();
		}
	};



}
